package com.domain.utils;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;

import java.io.InputStream;

public class UploadFileUntil {
    private static String accessKey = PropertiesParse.getConfigPropertiesValue("access_key");
    private static String secretKey = PropertiesParse.getConfigPropertiesValue("secret_key");

    public static String UploadImg(InputStream inputStream, String name, String type) throws Exception {
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
        // 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
        // 3 生成cos客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        // 设置 bucket名
        String bucketName = "bgm-1258164185";
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType(type);
        meta.setContentLength(inputStream.available());
        meta.setCacheControl("no-cache");
        meta.setContentEncoding("utf-8");
        String key = "/bgm/" + name;
        PutObjectResult result = cosclient.putObject(bucketName, key, inputStream, meta);
        String etag = result.getETag();
        System.out.println(etag);
        return etag;
    }

    public static void DelUploadImg(String key) {
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
        // 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
        // 3 生成cos客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        // 设置 bucket 名
        String bucketName = "bgm-1258164185";
        cosclient.deleteObject(bucketName, key);
    }

    public static void main(String[] args) {
        DelUploadImg("1543476861451573681");
    }
}
